const app = getApp();
const regeneratorRuntime = global.regeneratorRuntime = require("../libs/regenerator");
const co = require('../libs/co');
const passport = require("../services/passport/passport");

Page({

    onLoad: function(options) {
        app.launchOptions = options;
        /* !Do not Remove the follow line, used for build tool.*/
        /*{ComponentLaunchJs}*/
        this.loadCategoryList();
        console.log(this.selectedCategory);
        this.loadProducts(this.selectedCategory);
    },
    data: {
        inputShowed: false,
        inputVal: "",
        selectedCategory: 1,
        selectedCategoryId: "category_1",
        hidden: false
    },
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    hideInput: function () {
        this.setData({
            inputVal: "",
            inputShowed: false
        });
    },
    clearInput: function () {
        this.setData({
            inputVal: ""
        });
    },
    inputTyping: function (e) {
        this.setData({
            inputVal: e.detail.value
        });
    },
    loadCategoryList: function () {
        var that = this;
        co(function *() {
            let productService = require("../services/mall/productService");
            let categories = yield productService.getCategoryList();
            console.log(categories);
            that.setData({
                hidden: true,
                categories: categories
            });
        });
    },
    loadProducts: function (categoryId) {
      var that = this;
      co(function *() {
          let productService = require("../services/mall/productService");
          let products = yield productService.getProductsByCategory(categoryId);
          console.log(products);
          that.setData({
              hidden: true,
              products: products
          });
      });
    },
    clickitem: function (e) {   //带着specId 去详情界面
        var specId = e.currentTarget.dataset.specid;
        wx.navigateTo({
            url: '/pages/page9?id=' + specId,
        })
    },
    onReady:function(){
      this.setData({
        selectedCategory : 1
      });
    },

    onSelectCateogry: function(e){
      var index = e.target.dataset.index;
      if (index != this.selectedCategory) {
        this.loadProducts(index);
      }
      this.setData({
        hidden: true,
        selectedCategory: index,
        selectedCategoryId: "category_"+index
      });
    },
    /* !Do not Remove the follow line, used for build tool.*/
    /*{ComponentJs}*/

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }

});
